package com.muyu.system.service.impl;

import com.muyu.common.core.utils.DateUtils;
import com.muyu.system.domain.DmUserAddress;
import com.muyu.system.mapper.DmUserAddressMapper;
import com.muyu.system.service.IDmUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户地址Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-17
 */
@Service
public class DmUserAddressServiceImpl implements IDmUserAddressService {
    @Autowired
    private DmUserAddressMapper dmUserAddressMapper;

    /**
     * 查询用户地址
     *
     * @param id 用户地址主键
     * @return 用户地址
     */
    @Override
    public DmUserAddress selectDmUserAddressById(Long id) {
        return dmUserAddressMapper.selectDmUserAddressById(id);
    }

    /**
     * 查询用户地址列表
     *
     * @param dmUserAddress 用户地址
     * @return 用户地址
     */
    @Override
    public List<DmUserAddress> selectDmUserAddressList(DmUserAddress dmUserAddress) {
        return dmUserAddressMapper.selectDmUserAddressList(dmUserAddress);
    }

    /**
     * 新增用户地址
     *
     * @param dmUserAddress 用户地址
     * @return 结果
     */
    @Override
    public int insertDmUserAddress(DmUserAddress dmUserAddress) {
        dmUserAddress.setCreateTime(DateUtils.getNowDate());
        if (dmUserAddress.getIsDefault() != null && dmUserAddress.getIsDefault() == 0) {
            DmUserAddress dmUserAddressquery = new DmUserAddress();
            dmUserAddressquery.setUserId(dmUserAddress.getUserId());
            List<DmUserAddress> dmUserAddressList = dmUserAddressMapper.selectDmUserAddressList(dmUserAddressquery);
            for (DmUserAddress userAddress : dmUserAddressList) {
                userAddress.setIsDefault(1L);
                dmUserAddressMapper.updateDmUserAddress(userAddress);
            }
        }
        return dmUserAddressMapper.insertDmUserAddress(dmUserAddress);
    }

    /**
     * 修改用户地址
     *
     * @param dmUserAddress 用户地址
     * @return 结果
     */
    @Override
    public int updateDmUserAddress(DmUserAddress dmUserAddress) {
        dmUserAddress.setUpdateTime(DateUtils.getNowDate());
        if (dmUserAddress.getIsDefault() == 0) {
            DmUserAddress dmUserAddressquery = new DmUserAddress();
            dmUserAddressquery.setUserId(dmUserAddress.getUserId());
            List<DmUserAddress> dmUserAddressList = dmUserAddressMapper.selectDmUserAddressList(dmUserAddressquery);
            for (DmUserAddress userAddress : dmUserAddressList) {
                userAddress.setIsDefault(1L);
                dmUserAddressMapper.updateDmUserAddress(userAddress);
            }
        }
        return dmUserAddressMapper.updateDmUserAddress(dmUserAddress);
    }

    /**
     * 批量删除用户地址
     *
     * @param ids 需要删除的用户地址主键
     * @return 结果
     */
    @Override
    public int deleteDmUserAddressByIds(Long[] ids) {
        return dmUserAddressMapper.deleteDmUserAddressByIds(ids);
    }

    /**
     * 删除用户地址信息
     *
     * @param id 用户地址主键
     * @return 结果
     */
    @Override
    public int deleteDmUserAddressById(Long id) {
        return dmUserAddressMapper.deleteDmUserAddressById(id);
    }
}
